package cn.dlc.myapplication;

/************************************ 蓝牙配对函数 * **************/

import android.bluetooth.BluetoothDevice;
import android.util.Log;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class BtPairUtil {
    /**
     * 与设备配对 参考源码：platform/packages/apps/Settings.git
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
     */
    static public boolean createBond(Class btClass, BluetoothDevice btDevice) {
        try {
            Method createBondMethod = btClass.getMethod("createBond");
            Boolean returnValue = (Boolean) createBondMethod.invoke(btDevice);
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 与设备解除配对 参考源码：platform/packages/apps/Settings.git
     * /Settings/src/com/android/settings/bluetooth/CachedBluetoothDevice.java
     */
    static public boolean removeBond(Class<?> btClass, BluetoothDevice btDevice) {
        try {
            Method removeBondMethod = btClass.getMethod("removeBond");
            Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice);
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 设置pin码
     *
     * @param btClass
     * @param btDevice
     * @param str
     * @return
     */
    static public boolean setPin(Class<? extends BluetoothDevice> btClass, BluetoothDevice btDevice,
        String str) {
        try {
            Method removeBondMethod =
                btClass.getDeclaredMethod("setPin", new Class[] { byte[].class });
            Boolean returnValue =
                (Boolean) removeBondMethod.invoke(btDevice, new Object[] { str.getBytes() });
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 取消用户输入  
    static public boolean cancelPairingUserInput(Class<?> btClass, BluetoothDevice device) {
        try {
            Method createBondMethod = btClass.getMethod("cancelPairingUserInput");
            //        cancelBondProcess(btClass, device);
            Boolean returnValue = (Boolean) createBondMethod.invoke(device);
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 取消配对  
    static public boolean cancelBondProcess(Class<?> btClass, BluetoothDevice device) {
        try {
            Method createBondMethod = btClass.getMethod("cancelBondProcess");
            Boolean returnValue = (Boolean) createBondMethod.invoke(device);
            return returnValue;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //确认配对

    static public void setPairingConfirmation(Class<?> btClass, BluetoothDevice device,
        boolean isConfirm) {
        try {
            Method setPairingConfirmation =
                btClass.getDeclaredMethod("setPairingConfirmation", boolean.class);
            setPairingConfirmation.invoke(device, isConfirm);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param clsShow
     */
    static public void printAllInform(Class clsShow) {
        try {
            // 取得所有方法  
            Method[] hideMethod = clsShow.getMethods();
            int i = 0;
            for (; i < hideMethod.length; i++) {
                Log.e("method name", hideMethod[i].getName() + ";and the i is:" + i);
            }
            // 取得所有常量  
            Field[] allFields = clsShow.getFields();
            for (i = 0; i < allFields.length; i++) {
                Log.e("Field name", allFields[i].getName());
            }
        } catch (SecurityException e) {
            // throw new RuntimeException(e.getMessage());  
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // throw new RuntimeException(e.getMessage());  
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
    }
}  
